package com.lg.online_shop.controller;

import com.lg.online_shop.entity.Address;
import com.lg.online_shop.mapper.AddressMapper;
import com.lg.online_shop.utils.Result;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressMapper addressMapper;


//    根据userId获取地址
    @GetMapping("/getAddressByUserId")
    public Result<List<Address>> getAddressByUserId(Integer userId) {
        List<Address> addressList = addressMapper.selectAddressByUserId(userId);
       return Result.success(addressList);
    }


//    删除地址
    @GetMapping("/deleteAddress")
    public Result<String> deleteAddress(Integer id) {
        addressMapper.deleteById(id);
        return Result.success("删除成功");
    }


//    新增地址
    @GetMapping("/addAddress")
    public Result addAddress(@Param("addressName") String addressName, @Param("userId") Integer userId, @Param("phone") String phone, @Param("addressee") String addressee) {

         int i = addressMapper.addAddress(addressName, userId, phone, addressee);

        return i == 1 ?  Result.success("添加成功") : Result.error("添加失败");
    }

//    修改地址
    @GetMapping("/updateAddress")
    public Result updateAddress(Integer id, String addressName, String phone, String addressee) {

         Integer i =  addressMapper.updateAddress( id, addressName, phone, addressee);

        return i == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }
}
